﻿using Microsoft.AspNetCore.Http;
using System.IO;
using System.Threading.Tasks;

namespace Akka.Ext.Http.Mvc
{
    internal class CommonUtils
    {

        internal static async Task<RequestInfo> CreateRequestInfo(HttpRequest httpRequest, RequestInfo req)
        {
            req.Url = httpRequest.Path;
            foreach (var item in httpRequest.Query)
            {
                req.Query.Add(item.Key, item.Value);
            }
            if (httpRequest.Method.ToLower() == "get")
            {
                req.Method = RestSharp.Method.Get;
            }
            else
            {
                req.Method = RestSharp.Method.Post;
                if (httpRequest.ContentType == "application/json")
                {
                    using (var reader = new StreamReader(httpRequest.Body))
                    {
                        req.JsonBody = await reader.ReadToEndAsync();
                    }
                    await httpRequest.Body.DisposeAsync();
                }
                else
                {
                    var formCol = await httpRequest.ReadFormAsync();
                    foreach (var item in formCol.Files)
                    {
                        using (var memoryStream = new MemoryStream())
                        {
                            await item.CopyToAsync(memoryStream);
                            var upload = new UploadFileInfo
                            {
                                Name = item.Name,
                                FileName = item.FileName,
                                BytesData = memoryStream.ToArray()
                            };
                            req.Files.Add(upload);
                        }
                    }
                    foreach (var item in formCol)
                    {
                        req.Form.Add(item.Key, item.Value);
                    }
                }
            }
            return req;
        }

        internal static async Task CreateResponseInfo(HttpResponse httpResponse, ResponseInfo res)
        {
            if (res.IsSuccess)
            {
                if (res.ContentType == "application/octet-stream") //文件下载
                {
                    httpResponse.ContentType = "application/octet-stream";
                    var filename = System.Net.WebUtility.UrlEncode(res.FileName);
                    httpResponse.Headers.Add("Content-Disposition", $"attachment;filename={filename};filename*=utf-8''{filename}");
                    await httpResponse.Body.WriteAsync(res.DataBytes, 0, res.DataBytes.Length);
                }
                else //文本或者json
                {
                    if (res.ContentType.StartsWith("text/plain"))
                    {
                        httpResponse.ContentType = "text/plain; charset=utf-8";

                    }
                    else if (res.ContentType.StartsWith("application/json"))
                    {
                        httpResponse.ContentType = "application/json; charset=utf-8";
                    }
                    await httpResponse.WriteAsync(res.Data);
                }
            }
        }
    }
}
